package com.zkthink.ceres.member.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zkthink.base.R;
import com.zkthink.base.request.PageParams;
import com.zkthink.ceres.member.dto.MemberAddTagDTO;
import com.zkthink.ceres.member.dto.MemberForbiddenDTO;
import com.zkthink.ceres.member.dto.MemberPageDTO;
import com.zkthink.ceres.member.entity.Member;
import com.zkthink.ceres.member.service.MemberService;
import com.zkthink.ceres.member.service.MemberTagRelService;
import com.zkthink.ceres.member.vo.AdminMemberInfoVO;
import com.zkthink.ceres.member.vo.MemberTagVO;
import com.zkthink.ceres.member.vo.MemberVO;
import com.zkthink.dozer.DozerUtils;
import com.zkthink.security.annotation.PreAuth;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 前端控制器 商城会员信息
 * </p>
 *
 * @author kellen
 * @date 2020-05-02
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/member")
@Api(value = "Member", tags = "商城会员信息")
@PreAuth(replace = "member:")
@AllArgsConstructor
public class MemberController {

  private MemberService memberService;

  private DozerUtils dozerUtils;

  private MemberTagRelService tagRelService;





  /**
   * 分页查询
   */
  @ApiOperation(value = "分页列表查询")
  @PostMapping(value = "/page")
  public R<IPage<Member>> page(@RequestBody @Validated PageParams<MemberPageDTO> params) {
    // 处理参数
    return R.success(memberService.page(params));
  }


  /**
   * 加入黑名单
   */
  @ApiOperation(value = "加入黑名单")
  @PostMapping(value = "/forbidden")
  public R forbidden(@RequestBody @Validated MemberForbiddenDTO updateDTO) {
    //查询会员信息
    Member member = memberService.findById(updateDTO.getMid());
    if (null == member) {
      return R.validFail("客户不存在");
    }
//    if (member.getStatus().equals(updateDTO.getStatus())) {
//      return R.validFail("status参数非法");
//    }
    member.setStatus(updateDTO.getStatus());
    memberService.updateById(member);
    return R.success();
  }


  /**
   * 打标签
   */
  @ApiOperation(value = "打标签")
  @PostMapping(value = "/addTag")
  public R addTag(@RequestBody MemberAddTagDTO addTagDTO) {
    memberService.addTag(addTagDTO);
    return R.success();
  }


  /**
   * 查询客户详细
   */
  @ApiOperation(value = "查询客户详细")
  @GetMapping(value = "/query/{mid}")
  public R<AdminMemberInfoVO> query(@PathVariable("mid") Long mid) {
    //查询会员信息
    Member member = memberService.findById(mid);
    if (null == member) {
      return R.validFail("会员不存在");
    }
    MemberVO memberVO = dozerUtils.map(member, MemberVO.class);
    //查询标签信息
    List<MemberTagVO> tagVOList = tagRelService.findByMid(mid);
    AdminMemberInfoVO adminMemberInfoVO = AdminMemberInfoVO.builder().memberBaseInfo(memberVO)
        .memberTagList(tagVOList).build();
    return R.success(adminMemberInfoVO);
  }


}
